import * as Router from '@/router/index';
import * as status from '@/utils/status.js';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import useAppStore from '@/stores/app';

NProgress.configure({ showSpinner: false });

let appStore = null;

Router.default.beforeEach((to, from, next) => {
  NProgress.start();
  // 修改页面title标题，如页面需要单设标题使用to.meta.title或者页面加载后的钩子里改document.title
  document.title = to.meta && to.meta.title ? to.meta.title : status.GLOBAL.projectTitle;
  appStore = appStore ? appStore : useAppStore();
  if (appStore.isAddRoutes){
    // 已经按下面else中的条件过滤了路由，直接放行
    next();
  } else {
    appStore.changeRouter(Router);
    // 重新进入路由
    next({...to, replace: true});
  }
});

Router.default.afterEach(() => {
  NProgress.done();
});